package com.videoaggre.zcl.ui.activity

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ComponentActivity
import androidx.lifecycle.lifecycleScope
import com.videoaggre.zcl.api.ApiObserver
import com.videoaggre.zcl.api.ApiRequest
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job

import android.content.Intent
import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import com.videoaggre.zcl.helper.OnStartActivityListener

typealias ActivityForResult = (ActivityResult) -> Unit
typealias RequestMultiplePermissions = (MutableMap<String, Boolean>) -> Unit

fun <T> ComponentActivity.launchUI(
    block: suspend CoroutineScope.() -> T, apiObserver: ApiObserver<T>? = null, error: ((Throwable) -> Unit)? = null, complete: (() -> Unit)? = null
): Job = ApiRequest(lifecycleScope).launchUI(block, error, complete, apiObserver)

open class BaseActivity(private var layoutResID: Int? = null) : AppCompatActivity(), OnStartActivityListener, View.OnClickListener {
    private var mActivityForResult: ActivityForResult? = null
    private var mIsNeedResultOk: Boolean? = null
    private var mRequestMultiplePermissions: RequestMultiplePermissions? = null

    // 这种设计同时只能有一个在做ActivityForResult操作否则上一个未完成的会被覆盖
    private val mStartActivityForResultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { activityResult ->
        if (mIsNeedResultOk == true) {
            if (activityResult.resultCode == RESULT_OK)
                mActivityForResult?.invoke(activityResult)
        } else {
            mActivityForResult?.invoke(activityResult)
        }
    }
    private val mRequestMultiplePermissionsLauncher = registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) { activityResult ->
        mRequestMultiplePermissions?.invoke(activityResult)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        com.yanzhenjie.sofia.Utils.setStatusBarDarkFont(this, true)
        if (layoutResID != null) {
            setContentView(layoutResID!!)
        } else {
            init()
        }
    }

    override fun setContentView(layoutResID: Int) {
        super.setContentView(layoutResID)
        init()
    }

    private fun init() {
        initView()
        initData()
    }

    open fun initView() {}
    open fun initData() {}

    fun bindViewClick(vararg views: View) = com.videoaggre.zcl.common.bindViewClick(this, *views)

    /**
     * 启动页面等待回调,回调为最后启动的回调
     * @param isNeedResultOk 是否需要返回RESULT_OK才进行回调
     */
    open fun launchStartActivityForResult(clazz: Class<*>, options: Bundle? = null, isNeedResultOk: Boolean = false, activityForResult: ActivityForResult? = null) {
        val intent = Intent(this, clazz)
        options?.let { intent.putExtras(it) }
        mActivityForResult = activityForResult
        mIsNeedResultOk = isNeedResultOk
        mStartActivityForResultLauncher.launch(intent)
    }

    open fun launchRequestMultiplePermissions(permissions: Array<String>, requestMultiplePermissions: RequestMultiplePermissions? = null) {
        mRequestMultiplePermissions = requestMultiplePermissions
        mRequestMultiplePermissionsLauncher.launch(permissions)
    }

    override fun goActicity(clazz: Class<*>, bundle: Bundle?, requestCode: Int?) {
        val intent = Intent(this, clazz)
        bundle?.let { intent.putExtras(it) }
        if (requestCode != null) {
            startActivityForResult(intent, requestCode)
        } else {
            startActivity(intent)
        }
    }

    override fun goActicityFinish(clazz: Class<*>, bundle: Bundle?, requestCode: Int?) {
        goActicity(clazz, bundle, requestCode)
        finish()
    }

    override fun onClick(v: View?) {

    }

}